package com.likou;

/**
 * Author: 陈桂明
 * Date: 2025/4/1 14:48
 * Description: q11
 */
public class q11 {
    public static void main(String[] args) {
        int maxArea = new q11().maxArea(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7});
        System.out.println(maxArea);

    }

    // 超时
    // public int maxArea(int[] height) {
    //     if (height.length <= 1) {
    //         return 0;
    //     }
    //     int max = 0;
    //     for (int i = 0; i < height.length; i++) {
    //         int a = height[i];
    //         for (int j = i + 1; j < height.length; j++) {
    //             int b = height[j];
    //             int s = (j - i) * (Math.min(a, b));
    //             if (s > max) {
    //                 max = s;
    //             }
    //         }
    //     }
    //
    //     return max;
    // }

    public int maxArea(int[] height) {
        if (height.length <= 1) {
            return 0;
        }

        int left = 0;
        int right = height.length - 1;
        int max = 0;
        while (left != right) {
            int a = height[left];
            int b = height[right];
            int h = Math.min(a, b);
            int s = (right - left) * h;
            max = Math.max(s, max);

            if (a < b) {
                while (height[left] <= h && left < right) {
                    left++;
                }
            } else {
                while (height[right] <= h && left < right) {
                    right--;
                }
            }
        }
        return max;
    }
}
